<template>
  <div class="vote-statistics">
    <!-- 数据概览卡片 -->
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>总投票数</span>
              <el-tag type="success">累计</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="number">12,580</div>
            <div class="compare">
              <span>同比上月</span>
              <span class="up">↑15.8%</span>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>参与人数</span>
              <el-tag type="warning">月度</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="number">3,256</div>
            <div class="compare">
              <span>同比上月</span>
              <span class="up">↑12.3%</span>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>进行中投票</span>
              <el-tag type="primary">实时</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="number">5</div>
            <div class="compare">
              <span>较上周</span>
              <span class="up">↑2</span>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover" class="data-card">
          <template #header>
            <div class="card-header">
              <span>平均参与度</span>
              <el-tag>月度</el-tag>
            </div>
          </template>
          <div class="card-content">
            <div class="number">68.5%</div>
            <div class="compare">
              <span>同比上月</span>
              <span class="down">↓3.2%</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="mt-20">
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>投票趋势分析</span>
              <el-radio-group v-model="trendTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="year">本年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div ref="trendChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>投票类型分布</span>
              <el-select v-model="typeTimeRange" size="small" style="width: 120px">
                <el-option label="最近7天" value="week" />
                <el-option label="最近30天" value="month" />
                <el-option label="最近90天" value="quarter" />
              </el-select>
            </div>
          </template>
          <div ref="pieChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 排行榜 -->
    <el-row :gutter="20" class="mt-20">
      <el-col :span="12">
        <el-card class="rank-card">
          <template #header>
            <div class="card-header">
              <span>热门投票排行</span>
              <el-button type="primary" link>查看更多</el-button>
            </div>
          </template>
          <el-table :data="hotVoteList" style="width: 100%">
            <el-table-column type="index" label="排名" width="80">
              <template #default="scope">
                <div class="rank-number" :class="getRankClass(scope.$index)">
                  {{ scope.$index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="title" label="投票标题" />
            <el-table-column prop="votes" label="票数" width="120">
              <template #default="scope">
                {{ scope.row.votes.toLocaleString() }}
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="rank-card">
          <template #header>
            <div class="card-header">
              <span>活跃用户排行</span>
              <el-button type="primary" link>查看更多</el-button>
            </div>
          </template>
          <el-table :data="activeUserList" style="width: 100%">
            <el-table-column type="index" label="排名" width="80">
              <template #default="scope">
                <div class="rank-number" :class="getRankClass(scope.$index)">
                  {{ scope.$index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="username" label="用户名" />
            <el-table-column prop="participations" label="参与次数" width="120" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import * as echarts from 'echarts'

// 时间范围选择
const trendTimeRange = ref('month')
const typeTimeRange = ref('month')

// 图表引用
const trendChartRef = ref(null)
const pieChartRef = ref(null)
let trendChart = null
let pieChart = null

// 排行榜数据
const hotVoteList = ref([
  { title: '2024年最可爱宠物评选', votes: 2580 },
  { title: '春季宠物美容活动投票', votes: 1860 },
  { title: '宠物医疗服务满意度调查', votes: 1350 },
  { title: '最受欢迎宠物品种评选', votes: 980 },
  { title: '宠物用品品牌调查', votes: 760 }
])

const activeUserList = ref([
  { username: '爱宠人士A', participations: 128 },
  { username: '铲屎官B', participations: 96 },
  { username: '宠物达人C', participations: 85 },
  { username: '萌宠控D', participations: 76 },
  { username: '爱心志愿者E', participations: 65 }
])

// 初始化趋势图表
const initTrendChart = () => {
  if (!trendChartRef.value) return
  
  trendChart = echarts.init(trendChartRef.value)
  
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['投票数', '参与人数']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '投票数',
        type: 'line',
        smooth: true,
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        areaStyle: {}
      },
      {
        name: '参与人数',
        type: 'line',
        smooth: true,
        data: [420, 532, 501, 534, 690, 730, 620],
        areaStyle: {}
      }
    ]
  }
  
  trendChart.setOption(option)
}

// 初始化饼图
const initPieChart = () => {
  if (!pieChartRef.value) return
  
  pieChart = echarts.init(pieChartRef.value)
  
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 10,
      data: ['宠物评选', '活动投票', '满意度调查', '其他']
    },
    series: [
      {
        name: '投票类型',
        type: 'pie',
        radius: ['50%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: [
          { value: 1048, name: '宠物评选' },
          { value: 735, name: '活动投票' },
          { value: 580, name: '满意度调查' },
          { value: 284, name: '其他' }
        ]
      }
    ]
  }
  
  pieChart.setOption(option)
}

// 获取排名样式
const getRankClass = (index) => {
  const classes = ['rank-first', 'rank-second', 'rank-third']
  return classes[index] || ''
}

// 监听窗口大小变化
window.addEventListener('resize', () => {
  trendChart?.resize()
  pieChart?.resize()
})

// 组件挂载时初始化图表
onMounted(() => {
  initTrendChart()
  initPieChart()
})
</script>

<style scoped>
.vote-statistics {
  padding: 20px;
}

.mt-20 {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.data-card .card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.data-card .card-content .number {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 10px;
}

.data-card .card-content .compare {
  font-size: 12px;
  color: #666;
}

.data-card .card-content .compare .up {
  color: #67c23a;
  margin-left: 8px;
}

.data-card .card-content .compare .down {
  color: #f56c6c;
  margin-left: 8px;
}

.chart-card .card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-card .chart-container {
  height: 350px;
}

.rank-card .card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.rank-card .rank-number {
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  border-radius: 50%;
  margin: 0 auto;
}

.rank-card .rank-number.rank-first {
  background-color: #f56c6c;
  color: #fff;
}

.rank-card .rank-number.rank-second {
  background-color: #e6a23c;
  color: #fff;
}

.rank-card .rank-number.rank-third {
  background-color: #409eff;
  color: #fff;
}
</style> 